<?php
/**
*总监售后数据
*/
require("../include/init.php");
//连接数据库
$mysql=mysql::getIns();
date_default_timezone_set('Asia/Shanghai');
//今天的时间戳

$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
  $address=$v['areaname'];
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));

  //统计今天售后区人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $nowservice=$mysql->getOne($sql);

  //昨天的时间戳
  $t = time();
  $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t))-24*3600;
  $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t))-24*3600;

  //昨天售后区人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $yesdservice=$mysql->getOne($sql);




  /****************
  *获取本周开始时间和结束时间
  *
  ***********/

  $date=date('Y-m-d');  //当前日期
  $first=1; //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
  $w=date('w',strtotime($date));  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  $now_start=date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')); //获取本周开始日期，如果$w是0，则表示周日，减去 6 天
  $now_end=date('Y-m-d',strtotime("$now_start +6 days"));  //本周结束日期
  $one=strtotime($now_start);
  $two=$one+24*3600*1;
  $three=$one+24*3600*2;
  $four=$one+24*3600*3;
  $five=$one+24*3600*4;
  $six=$one+24*3600*5;
  $seven=$one+24*3600*6;
  $nowone=$one+24*3600*7;

  //获取下周周一
  $nowone=strtotime('-1 monday', time())+24*3600*7;
  // echo date('Y-m-d H:i:s',$nowone);


  //统计本周周一进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $one and timestamp < $two and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $onejk=$mysql->getOne($sql);
  //统计本周周二进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $two and timestamp < $three and area<>'展厅区' and area<>'销售区' and area<>'精品区' and area<>'精品区' and shop='". $address ."'";
  $twojk=$mysql->getOne($sql);
  //统计本周周三进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $three and timestamp < $four and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $threejk=$mysql->getOne($sql);
  //统计本周周四进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $four and timestamp < $five and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $fourjk=$mysql->getOne($sql);
  //统计本周周五进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $five and timestamp < $six and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $fivejk=$mysql->getOne($sql);
  // echo $fivejk;
  // exit;
  //统计本周周六进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  // $sql="select * from alldev where whitelist <> '1' and timestamp >= $six and timestamp < $seven and shop='". $address ."'";
  $sixjk=$mysql->getOne($sql);
  // echo $sixjk;
  // exit;
  //统计本周周日进店量
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $seven and timestamp < $nowone and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $sevenjk=$mysql->getOne($sql);




  /**************************
  获取本月第一个周一的时间

  **********************/
  $year=date('Y',time());
  $month=date('m',time());
  $base_time = strtotime ( "{$year}-{$month}-01" );
  $base_w = date ( "w", $base_time );

  if($base_w == '1'){
    //刚好是星期一
    $first = $base_time;
  }else{
    //不是星期一
    //找到上个星期天是几月几号
    $last_sun = date ( "Y-m-d", strtotime("-$base_w day", $base_time));
    //在上个星期天的基础上 加8天
    $first = strtotime ( "+8 day", strtotime ( $last_sun ) );
  }
  //第二个周一的时间
  $second=$first+7*24*3600;
  // echo date('Y-m-d H:i:s',$second);
  // exit;
  //第三个时间戳
  $third=$second+7*24*3600;
  // echo date('Y-m-d H:i:s',$third);
  // exit;
  //第四个时间戳
  $fourth=$third+7*24*3600;
  // echo date('Y-m-d H:i:s',$fourth);
  // exit;
  //下周一时间戳
  $fifth=$fourth+7*24*3600;
  // echo date('Y-m-d H:i:s',$third);
  // exit;


  //获取这个月第一周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $first and timestamp < $second and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $firstjk=$mysql->getOne($sql);
  // echo $firstjk;
  // exit;

  //获取这个月第二周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $second and timestamp < $third and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $secondjk=$mysql->getOne($sql);
  // echo $secondjk;
  // exit;
  //存储数据供下边删除重复的
  $bak=$secondjk;

  //获取这个月第三周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $third and timestamp < $fourth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $thirdjk=$mysql->getOne($sql);
  // echo $thirdjk;
  // exit;

  //获取这个月第四周的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourth and timestamp < $fifth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $fourthjk=$mysql->getOne($sql);

  //获取第一季度开始时间戳
  $firstmonth =mktime(0,0,0,1,1,date('Y',time()));

  //获取第二季度开始时间
  $secondmonth =mktime(0,0,0,4,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$secondmonth);

  //获取第三季度开始时间
  $thirdmonth =mktime(0,0,0,7,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$thirdmonth);

  //获取第四季度开始时间
  $fourthmonth =mktime(0,0,0,10,1,date('Y',time()));
  // echo date('Y-m-d H:i:s',$fourthmonth);

  //获取明年第一季度开始时间
  $firstmonths =mktime(0,0,0,1,1,date('Y',time())+1);
  // echo date('Y-m-d H:i:s',$firstmonths);



  //获取第一季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $firstmonth and timestamp < $secondmonth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $firstmonth=$mysql->getOne($sql);
  // echo $firstmonth;
  // exit;

  //获取第二季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $secondmonth and timestamp < $thirdmonth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $secondmonth=$mysql->getOne($sql);

  //获取第三季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $thirdmonth and timestamp < $fourthmonth and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $thirdmonth=$mysql->getOne($sql);
  // echo $thirdmonth;
  // exit;


  //获取第四季度的人数
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp >= $fourthmonth and timestamp < $firstmonths and area<>'展厅区' and area<>'销售区' and area<>'精品区' and shop='". $address ."'";
  $fourthmonth=$mysql->getOne($sql);

  // echo $fourthmonth;
  // exit;

  /****************************************


  		4s店的wifi登录率


  ****************************************/


  //总人数为今天累计人数
  $maxtime=time();
  $mintime=$maxtime-10*60;



  //统计当前在线人数    (在线人数为当前时间的十分钟之前到现在的人数)


  $maxtime=time();
  $mintime=$maxtime-10*60;
  // echo $maxtime . '<br />';
  // echo $mintime;
  $sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";
  $allnow=$mysql->getOne($sql);


  //统计登陆人数
  $sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
  $denglu=$mysql->getOne($sql);
  // echo $login;

  // echo $allday;
  // exit;
  // echo $denglu;
  $loginnum=ceil(($denglu/$allnow)*100);
  // echo $loginnum;
  // exit;

  //未登录
  $nologinnum=100-$loginnum;



  switch ($address) {
    case '义乌市博奥汽车销售服务有限公司':
      $hash='Area_ServiceDirector_ywba';
      break;
    case '广东粤奥汽车销售服务有限公司':
      $hash='Area_ServiceDirector_gdya';
      break;
    case '辽宁鑫迪汽车销售服务有限公司':
      $hash='Area_ServiceDirector_lnxd';
      break;
    case '江苏天奥汽车销售服务有限公司':
        $hash='Area_ServiceDirector_jsta';
        break;
    case '成都新元素雅麓汽车销售服务有限公司':
      $hash='Area_ServiceDirector_cdyl';
      break;
    case '云南驰泰汽车销售服务有限公司':
      $hash='Area_ServiceDirector_ynct';
      break;
    case '宁夏奥立升汽车销售服务有限公司':
      $hash='Area_ServiceDirector_nxals';
      break;
    case '陕西奥诚汽车销售服务有限公司':
      $hash='Area_ServiceDirector_sxac';
      break;
    case '北京博瑞祥星汽车销售有限公司':
      $hash='Area_ServiceDirector_brxx';
      break;
  }
  $redis = new Redis();
  //连接服务器
  $redis->connect("localhost");
  //授权
  $redis->auth("redis=pwd@2016");

  $redis->hmset($hash,
  array(
    'nowservice'=>$nowservice,
    'yesdservice'=>$yesdservice,
    'onejk'=>$onejk,
    'twojk'=>$twojk,
    'threejk'=>$threejk,
    'fourjk'=>$fourjk,
    'fivejk'=>$fivejk,
    'sixjk'=>$sixjk,
    'sevenjk'=>$sevenjk,
    'firstjk'=>$firstjk,
    'secondjk'=>$secondjk,
    'thirdjk'=>$thirdjk,
    'fourthjk'=>$fourthjk,
    'firstmonth'=>$firstmonth,
    'secondmonth'=>$secondmonth,
    'thirdmonth'=>$thirdmonth,
    'fourthmonth'=>$fourthmonth,
    'loginnum'=>$loginnum,
    'nologinnum'=>$nologinnum,
  ));
}
